var settingTree;
var olddata;
//人员树默认ztree参数
var setting = {
		view : {
			selectedMulti : true
		// 设置是否允许同时选中多个节点
		},
		edit : {
			enable : false, // 设置 zTree 是否处于编辑状态
			editNameSelectAll : false, // 节点编辑名称 input 初次显示时,设置 txt
										// 内容是否为全选状态
		},
		data : {
			simpleData : {
				enable : true
			// 确定 zTree 初始化时的节点数据、异步加载时的节点数据、或 addNodes 方法中输入的 newNodes
			// 数据是否采用简单数据模式 (Array)
			}
		},
		check : {
			chkStyle : "checkbox",// 勾选框类型
			enable : true, // 设置 zTree 的节点上是否显示 checkbox
			chkboxType : {
				"Y" : "ps",
				"N" : "ps"
			}
		},
		async : {
			enable : true,
			url : "../sysOrgController/loadStaffTreeData?t=" + (new Date().getTime()),
			autoParam : [ "id=pid" ]
		},
		callback: {
			onExpand: check
		}

	};
	//动态设置树的参数
	function settingTreeFunction(chkboxFather,chkboxChild){
		
		settingTree = {
				view: {
					selectedMulti: true	 //设置是否允许同时选中多个节点
				},
				edit: {
					enable: false, //设置 zTree 是否处于编辑状态
					editNameSelectAll: false, //节点编辑名称 input 初次显示时,设置 txt 内容是否为全选状态
				},
				data: {
					simpleData: {
						enable: true //确定 zTree 初始化时的节点数据、异步加载时的节点数据、或 addNodes 方法中输入的 newNodes 数据是否采用简单数据模式 (Array)
					}
				},
				check:{
					chkStyle:"checkbox",//勾选框类型
					enable:true, //设置 zTree 的节点上是否显示 checkbox
					chkboxType:{"Y":chkboxFather,"N":chkboxChild}
				},
				async:{
					enable: true,
					url:"../sysOrgController/loadOrgTreeData",
					autoParam:["id=pid"]
				},
				callback: {
					onExpand: check
				}

		
			};
		
	}
//保存数据权限
function saveBranch(){
	if(window.parent.hadPermission.update==false){
		alertModel("您无权修改");
		return;
	}
	var linkId=window.parent.$('#mdiframe').attr("linkId");
	var staffid=window.parent.$("#addDP").attr("staffid");
	var name=window.parent.$("#addDP").attr("gwname");
	var id=window.parent.$("#addDP").attr("gwid");
	var bindId=window.parent.$("#addDP").attr("bindid");
	var orgid=window.parent.$("#addDP").attr("orgid");
	var dataIntroduction=window.parent.$("#introduction").val();
	var	nodes;
	var dataDictionaryId=window.parent.$("#dataPermissionTypenew").val();
	var checked;//标识选中的树结构（关联下级，关联人员）
		if($("#linkBranch").is(':checked')){
			checked="Branch";
		}else{
			checked="notBranch";
		}
		 nodes=getOrgCheck();
		 if(nodes==null||nodes==""){
				alertModel("请选择组织范围");
				return ;
			}
	$.ajax({
			url:"../dataPermissionController/updateDataPermission",
			data:{"dataDictionaryId":dataDictionaryId,
				  "staffId":staffid,
				  "orgId":orgid,
				  "bindId":bindId,
				  "range":nodes,
				  "checked":checked,
				  "dataIntroduction":dataIntroduction,
				  "linkId":linkId
			},
		    traditional: true, 
			dataType:"json",
			type:"post",
			success:function(data){
				alertModel("保存成功");
				window.parent.$('#view').modal('hide');
				window.parent.getDp(window.parent.staffId,window.parent.orgId,window.parent.id);
			}
		})


}
function saveStaff(){
	if(window.parent.hadPermission.update==false){
		alertModel("您无权修改");
		return;
	}
	var linkId=window.parent.$('#mdiframe').attr("linkId");
	var staffid=window.parent.$("#addDP").attr("staffid");
	var name=window.parent.$("#addDP").attr("gwname");
	var id=window.parent.$("#addDP").attr("gwid");
	var bindId=window.parent.$("#addDP").attr("bindid");
	var orgid=window.parent.$("#addDP").attr("orgid");
	var dataIntroduction=window.parent.$("#introduction").val();
	var	nodes;
	var dataDictionaryId=window.parent.$("#dataPermissionTypenew").val();
	var checked;//标识选中的树结构（关联下级，关联人员）
		 checked = "Staff";
		 nodes=getStaffCheck();
		 if(nodes==null||nodes==""){
				alertModel("请选择到人员");
				return ;
			}
	$.ajax({
			url:"../dataPermissionController/updateDataPermission",
			data:{"dataDictionaryId":dataDictionaryId,
				  "staffId":staffid,
				  "orgId":orgid,
				  "bindId":bindId,
				  "range":nodes,
				  "checked":checked,
				  "dataIntroduction":dataIntroduction,
				  "linkId":linkId
			},
		    traditional: true, 
			dataType:"json",
			type:"post",
			success:function(data){
				alertModel("保存成功");
				window.parent.$('#view').modal('hide');
				window.parent.getDp(window.parent.staffId,window.parent.orgId,window.parent.id);
			}
		})


}
function Staff() {
		$.ajax({
			url : "../sysOrgController/loadStaffTreeData?t=" + (new Date().getTime()),
			data : {
				"pid" : null
			},
			async: false,
			dataType : "json",
			type : "post",
			success : function(data) {
				// 加载组织树
				$.fn.zTree.init($("#stafftree"), setting, data);
			}
		})
}
	//设置范围树是否关联下级
	function Branch(){
		if ($("#linkStaff").is(':checked')) {
			$("#linkStaff").attr("checked", false);
		}
		if ($("#linkBranch").is(':checked')) {
			var chkboxFather="ps";
			var chkboxChild="ps";
			//设置树的参数
			settingTreeFunction(chkboxFather,chkboxChild);
			
		}else{
			var chkboxFather="";
			var chkboxChild="";
			settingTreeFunction(chkboxFather,chkboxChild);
		}
		$.ajax({
			url:"../sysOrgController/loadOrgTreeData",
			data:{"pid":null},
			dataType:"json",
			type:"post",
			success:function(data){
				//加载组织树
				sysOrgTree = $.fn.zTree.init($("#orgtree"), settingTree, data);
			}
		})
		
		
	}
	//获取选中节点信息(组织树)
	function getOrgCheck(){
	var treeObj = $.fn.zTree.getZTreeObj("orgtree");
	var nodes = treeObj.getCheckedNodes(true);
	var node=treeObj.getCheckedNodes(true);
	if ($("#linkBranch").is(':checked')) {
		var t=0;
		for (var i = 0; i < node.length; i++) {
		if (node[i].getCheckStatus().half) {
			nodes.splice(i-t, 1);
			t=t+1;
		}
	}
	var infos = new Array();
	for (var i = 0; i < nodes.length; i++) {
		var pid = nodes[i].pId;
		if (pid != null) {
			pid = pid.toString();
			if ( treeObj.getNodeByParam("id", pid, null).getCheckStatus().half == true) {
				infos.push(nodes[i].id);
			}
		} else {
			infos.push(nodes[i].id)
		}
	}
		return infos;
	}else{
		var info = new Array();
		for (var i = 0; i < nodes.length; i++) {
			info.push(nodes[i].id);
		}
		return info;
	}
}
	function getStaffCheck() {
		var treeObj = $.fn.zTree.getZTreeObj("stafftree");
		var nodes = treeObj.getCheckedNodes(true);
		var info = new Array();
		for (var i = 0; i < nodes.length; i++) {
			if (false == nodes[i].isParent) {
				info.push((nodes[i].id).replace("u",""));
			}
		}
		return info;
		
	}
	//界面初始化
	$(function(){
		var linkId=window.parent.$('#mdiframe').attr("linkId");
		$('#staff-tab').one("click",Staff);
		$('#branch-tab').one("click",Branch);
		$.ajax({
			url:"../dataPermissionController/getOne",
			data:{"linkId":linkId},
			dataType:"json",
			type:"post",
			success:function(data){
				olddata=data;
				//如果关联组织下级
				if(data[0].linkBranch=="T"){
					$("#linkBranch").attr("checked", true);
					var chkboxFather="ps";
					var chkboxChild="ps";
					settingTreeFunction(chkboxFather,chkboxChild);
					$.ajax({
						url:"../sysOrgController/loadOrgTreeData",
						data:{"pid":null},
						dataType:"json",
						type:"post",
						success:function(info){
							//加载组织树
							sysOrgTree = $.fn.zTree.init($("#orgtree"), settingTree, info);
							check();
						}
					})
				}else if(data[0].linkBranch=="F"){
					var chkboxFather="";
					var chkboxChild="";
					settingTreeFunction(chkboxFather,chkboxChild);
					$.ajax({
						url:"../sysOrgController/loadOrgTreeData",
						data:{"pid":null},
						dataType:"json",
						type:"post",
						success:function(infos){
							//加载组织树
							sysOrgTree = $.fn.zTree.init($("#orgtree"), settingTree, infos);
							check();
						}
					})
				}else{
					$('#staff-tab').click();
					check();
				}
			}
		})
	
	})
	//反显所有勾选的节点
	function check(event, treeId, treeNode) {
	if(olddata[0].linkStaff=="T"){
		var treeObj = $.fn.zTree.getZTreeObj("stafftree");
		for (var i = 0; i < olddata.length; i++) {
			var id="u"+olddata[i].dataStaffId;
			node = treeObj.getNodeByParam("id", id, null);
			if(node!==null){
			treeObj.checkNode(node, true, true);
			}
		}
	}else{
		var treeObj = $.fn.zTree.getZTreeObj("orgtree");
		for (var i = 0; i < olddata.length; i++) {
			node = treeObj.getNodeByParam("id", olddata[i].dataOrgId, null);
			if(node!==null){
				treeObj.checkNode(node, true, true);
				}
		}
	}
}